package com.bootdo.api.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class StringUtil {
	private StringUtil() {
	}

	public static String handleChineseCode(String param) {
		try {
			if (StringUtils.isNotBlank(param)) {
				byte[] paramTmp = param.getBytes("ISO-8859-1");
				return new String(paramTmp, "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String handleChineseCode(String param,String sourceCharacterSet,String targetCharacterSet) {
		try {
			if (StringUtils.isNotBlank(param)) {
				byte[] paramTmp = param.getBytes(sourceCharacterSet);
				return new String(paramTmp, targetCharacterSet);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 将字符串转换为中文
	 * 
	 * @param param
	 * @return
	 */
	public static String handleToChinese(String param) {
		if (StringUtils.isNoneBlank(param)) {
			// 判断是否已经是中文
			boolean isChinese = Pattern.compile("[\u4e00-\u9fa5]").matcher(param).find();
			if (!isChinese) {
				return handleChineseCode(param);
			}
		}
		return param;
	}

	/**
	 * 将数字转化为大写
	 * 
	 * @param num
	 * @return
	 */
	public static String numToUpper(int num) {
		String u[] = { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
		char[] str = String.valueOf(num).toCharArray();
		String rstr = "";
		for (int i = 0; i < str.length; i++) {
			rstr = rstr + u[Integer.parseInt(str[i] + "")];
		}
		return rstr;
	}
	
	/**
	 * 16进制字符串转Byte[]
	 * @param hexString
	 * @return
	 */
	public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            
        }
        return d;
    }
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    
    /**
	 * inputstream 转 String
	 * @param is
	 * @return
	 */
	public static String convertStreamToString(InputStream is) {
		/*
		 * To convert the InputStream to String we use the
		 * BufferedReader.readLine() method. We iterate until the BufferedReader
		 * return null which means there's no more data to read. Each line will
		 * appended to a StringBuilder and returned as String.
		 */
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return sb.toString();
	}
	
	
	/**
	 * 校验身份证号的性别 （0：男  1：女）
	 * @param idNumber
	 * @return
	 */
	public static int checkSex(String idNumber) {
		int sex = 0;
		int length = idNumber.length();
		if (length == 18) {
			if (Integer.parseInt(idNumber.substring(16, 17)) % 2 == 0) {// 判断性别
				sex = 1;
			}
		} else {
			if (Integer.parseInt(idNumber.substring(14, 15)) % 2 == 0) {
				sex = 1;
			}
		}
		return sex;
	}
	
	
}